home *** CD-ROM | disk | FTP | other *** search
/ Freelog 115 / FreelogNo115-MaiJuin2013.iso / Internet / AvantBrowser / asetup.exe / _data / webkit / resources.pak / Unnamed File 000070.txt < prev    next >
Text File  |  2013-04-03  |  7KB  |  263 lines

  1. // Copyright (c) 2012 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4.  
  5. cr.define('cr.ui', function() {
  6.   /** @const */ var Command = cr.ui.Command;
  7.  
  8.   /**
  9.    * Creates a new menu item element.
  10.    * @param {Object=} opt_propertyBag Optional properties.
  11.    * @constructor
  12.    * @extends {HTMLDivElement}
  13.    */
  14.   var MenuItem = cr.ui.define('div');
  15.  
  16.   /**
  17.    * Creates a new menu separator element.
  18.    * @return {cr.ui.MenuItem} The new separator element.
  19.    */
  20.   MenuItem.createSeparator = function() {
  21.     var el = cr.doc.createElement('hr');
  22.     MenuItem.decorate(el);
  23.     return el;
  24.   };
  25.  
  26.   MenuItem.prototype = {
  27.     __proto__: HTMLButtonElement.prototype,
  28.  
  29.     /**
  30.      * Initializes the menu item.
  31.      */
  32.     decorate: function() {
  33.       var commandId;
  34.       if ((commandId = this.getAttribute('command')))
  35.         this.command = commandId;
  36.  
  37.       this.addEventListener('mouseup', this.handleMouseUp_);
  38.  
  39.       // Adding the 'custom-appearance' class prevents widgets.css from changing
  40.       // the appearance of this element.
  41.       this.classList.add('custom-appearance');
  42.  
  43.       this.setAttribute('role', 'menuitem');
  44.  
  45.       var iconUrl;
  46.       if ((iconUrl = this.getAttribute('icon')))
  47.         this.iconUrl = iconUrl;
  48.     },
  49.  
  50.     /**
  51.      * The command associated with this menu item. If this is set to a string
  52.      * of the form "#element-id" then the element is looked up in the document
  53.      * of the command.
  54.      * @type {cr.ui.Command}
  55.      */
  56.     command_: null,
  57.     get command() {
  58.       return this.command_;
  59.     },
  60.     set command(command) {
  61.       if (this.command_) {
  62.         this.command_.removeEventListener('labelChange', this);
  63.         this.command_.removeEventListener('disabledChange', this);
  64.         this.command_.removeEventListener('hiddenChange', this);
  65.         this.command_.removeEventListener('checkedChange', this);
  66.       }
  67.  
  68.       if (typeof command == 'string' && command[0] == '#') {
  69.         command = this.ownerDocument.getElementById(command.slice(1));
  70.         cr.ui.decorate(command, Command);
  71.       }
  72.  
  73.       this.command_ = command;
  74.       if (command) {
  75.         if (command.id)
  76.           this.setAttribute('command', '#' + command.id);
  77.  
  78.         this.label = command.label;
  79.         this.disabled = command.disabled;
  80.         this.hidden = command.hidden;
  81.  
  82.         this.command_.addEventListener('labelChange', this);
  83.         this.command_.addEventListener('disabledChange', this);
  84.         this.command_.addEventListener('hiddenChange', this);
  85.         this.command_.addEventListener('checkedChange', this);
  86.       }
  87.  
  88.       this.updateShortcut_();
  89.     },
  90.  
  91.     /**
  92.      * The text label.
  93.      * @type {string}
  94.      */
  95.     get label() {
  96.       return this.textContent;
  97.     },
  98.     set label(label) {
  99.       this.textContent = label;
  100.     },
  101.  
  102.     /**
  103.      * Menu icon.
  104.      * @type {string}
  105.      */
  106.     get iconUrl() {
  107.       return this.style.backgroundImage;
  108.     },
  109.     set iconUrl(url) {
  110.       this.style.backgroundImage = 'url(' + url + ')';
  111.     },
  112.  
  113.     /**
  114.      * @return {boolean} Whether the menu item is a separator.
  115.      */
  116.     isSeparator: function() {
  117.       return this.tagName == 'HR';
  118.     },
  119.  
  120.     /**
  121.      * Updates shortcut text according to associated command. If command has
  122.      * multiple shortcuts, only first one is displayed.
  123.      */
  124.     updateShortcut_: function() {
  125.       this.removeAttribute('shortcutText');
  126.  
  127.       if (!(this.command_ && this.command_.shortcut))
  128.         return;
  129.  
  130.       var shortcuts = this.command_.shortcut.split(/\s+/);
  131.  
  132.       if (shortcuts.length == 0)
  133.         return;
  134.  
  135.       var shortcut = shortcuts[0];
  136.       var mods = {};
  137.       var ident = '';
  138.       shortcut.split('-').forEach(function(part) {
  139.         var partUc = part.toUpperCase();
  140.         switch (partUc) {
  141.           case 'CTRL':
  142.           case 'ALT':
  143.           case 'SHIFT':
  144.           case 'META':
  145.             mods[partUc] = true;
  146.             break;
  147.           default:
  148.             console.assert(!ident, 'Shortcut has two non-modifier keys');
  149.             ident = part;
  150.         }
  151.       });
  152.  
  153.       var shortcutText = '';
  154.  
  155.       // TODO(zvorygin): if more cornercases appear - optimize following
  156.       // code. Currently 'Enter' keystroke is passed as 'Enter', and 'Space'
  157.       // is passed as 'U+0020'
  158.       if (ident == 'U+0020')
  159.         ident = 'Space';
  160.  
  161.       ['CTRL', 'ALT', 'SHIFT', 'META'].forEach(function(mod) {
  162.         if (mods[mod])
  163.           shortcutText += loadTimeData.getString('SHORTCUT_' + mod) + '+';
  164.       });
  165.  
  166.       if (ident.indexOf('U+') != 0) {
  167.         shortcutText +=
  168.             loadTimeData.getString('SHORTCUT_' + ident.toUpperCase());
  169.       } else {
  170.         shortcutText +=
  171.             String.fromCharCode(parseInt(ident.substring(2), 16));
  172.       }
  173.  
  174.       this.setAttribute('shortcutText', shortcutText);
  175.     },
  176.  
  177.     /**
  178.      * Handles mouseup events. This dispatches an activate event; if there is an
  179.      * associated command, that command is executed.
  180.      * @param {Event} e The mouseup event object.
  181.      * @private
  182.      */
  183.     handleMouseUp_: function(e) {
  184.       if (!this.disabled && !this.isSeparator() && this.selected) {
  185.         // Store |contextElement| since it'll be removed by {Menu} on handling
  186.         // 'activate' event.
  187.         var contextElement = this.parentNode.contextElement;
  188.         var activationEvent = cr.doc.createEvent('Event');
  189.         activationEvent.initEvent('activate', true, true);
  190.         activationEvent.originalEvent = e;
  191.         // Dispatch command event followed by executing the command object.
  192.         if (this.dispatchEvent(activationEvent)) {
  193.           var command = this.command;
  194.           if (command) {
  195.             command.execute(contextElement);
  196.             cr.ui.swallowDoubleClick(e);
  197.           }
  198.         }
  199.       }
  200.     },
  201.  
  202.     /**
  203.      * Updates command according to the node on which this menu was invoked.
  204.      * @param {Node=} opt_node Node on which menu was opened.
  205.      */
  206.     updateCommand: function(opt_node) {
  207.       if (this.command_) {
  208.         this.command_.canExecuteChange(opt_node);
  209.       }
  210.     },
  211.  
  212.     /**
  213.      * Handles changes to the associated command.
  214.      * @param {Event} e The event object.
  215.      */
  216.     handleEvent: function(e) {
  217.       switch (e.type) {
  218.         case 'disabledChange':
  219.           this.disabled = this.command.disabled;
  220.           break;
  221.         case 'hiddenChange':
  222.           this.hidden = this.command.hidden;
  223.           break;
  224.         case 'labelChange':
  225.           this.label = this.command.label;
  226.           break;
  227.         case 'checkedChange':
  228.           this.checked = this.command.checked;
  229.           break;
  230.       }
  231.     }
  232.   };
  233.  
  234.   /**
  235.    * Whether the menu item is disabled or not.
  236.    * @type {boolean}
  237.    */
  238.   cr.defineProperty(MenuItem, 'disabled', cr.PropertyKind.BOOL_ATTR);
  239.  
  240.   /**
  241.    * Whether the menu item is hidden or not.
  242.    * @type {boolean}
  243.    */
  244.   cr.defineProperty(MenuItem, 'hidden', cr.PropertyKind.BOOL_ATTR);
  245.  
  246.   /**
  247.    * Whether the menu item is selected or not.
  248.    * @type {boolean}
  249.    */
  250.   cr.defineProperty(MenuItem, 'selected', cr.PropertyKind.BOOL_ATTR);
  251.  
  252.   /**
  253.    * Whether the menu item is checked or not.
  254.    * @type {boolean}
  255.    */
  256.   cr.defineProperty(MenuItem, 'checked', cr.PropertyKind.BOOL_ATTR);
  257.  
  258.   // Export
  259.   return {
  260.     MenuItem: MenuItem
  261.   };
  262. });
  263.